# -*- coding: utf-8 -*-
from xTkinter import *
import tkFileDialog
import tkMessageBox as msgBox
import sys
from RequeteSQL import *
from RequeteSQL import *
RequeteSQL = RequeteSQL()
import xmlrpclib
class Vue_VoirEnregistrement(Frame):
    def __init__(self,master,parent,**cnf):
        self.ListeChamps=[]
        self.ListEntries=[]
        self.ListLabels=[]
        self.parent = parent
        self.master=master
        Frame.__init__(self,master,cnf)             
        self.pack(side=LEFT,anchor=NW)
        self.configure(width=800,height=600)        
        self.BoutonSuivant=xButton(self,text="Suivant",width=12, command=self.Suivant)
        self.BoutonPrecedant=xButton(self,text="Precedant",width=12, command=self.Precedant)
        self.BoutonSuivant.setLocation(270,0)
        self.BoutonPrecedant.setLocation(0,0)
        self.CBTables=xCombobox(self)
        self.CBTables.setLocation(135,0)
        self.CBTables.setWidth(15)
        self.CBTables.setListWidth(25)
        self.CBTables.setListHeight(5)
        self.obtenirTables() 
        self.CBTables.setValidator(self.obtenirChamps)
        self.CBTables.setText('<Choisir>')
        self.ListeEnregistrement = []
        self.Index = None
        
    def ajouterData(self):
        NomTable=self.CBTables.getText()
        Valeurs = []
        for i in range(len(self.ListeChamps)):
            Valeurs.append(self.ListEntries[i].get())
        Requete = RequeteSQL.sqlInsert(NomTable, Valeurs,self.ListeChamps )
        Reponse1 = self.parent.serveur.ExecuterRequeteSQL(Requete)
        if Reponse1[0]==1:
            Reponse2 = self.parent.serveur.ExecuterRequeteSQL('commit')
            if Reponse2[0]==1:
                msgBox.showinfo('PERP | Ajout d\'enregistrements', Reponse2[1])
            else:
                msgBox.showerror('PERP | Ajout d\'enregistrements', Reponse2[1])
        else:
            msgBox.showerror('PERP | Ajout d\'enregistrements', Reponse1[1])
        pass
    
    def obtenirTables(self):
        Reponse = self.parent.serveur.ObtenirListeTables(self.parent.user,self.parent.profil)
        if(Reponse[0]==1):
            self.ListeChamps = Reponse[1]
            for i in range(len(self.ListeChamps)):
                self.CBTables.addItem(self.ListeChamps[i][0])
                self.CBTables.setText(self.ListeChamps[i][0])
        
    def obtenirChamps(self):
        self.ListeChamps=[]
        for i in range(len(self.ListEntries)):
            self.ListEntries[i].destroy()
            self.ListLabels[i].destroy()

        self.ListEntries=[]
        self.ListLabels=[]
        table=self.CBTables.getText();
        Requete="SELECT COLUMN_NAME,DATA_TYPE,DATA_LENGTH,DATA_PRECISION,DATA_SCALE FROM USER_TAB_COLUMNS WHERE LOWER(TABLE_NAME)='"+table.lower()+"'"
        Reponse = self.parent.serveur.ExecuterRequeteSQLWhere(Requete)
        if(Reponse[0]==1):
            self.ListeChamps = Reponse[1]
            for i in range(len(self.ListeChamps)):
                self.ListLabels.append(xLabel(self,text=self.ListeChamps[i][0], width=len(self.ListeChamps[i][0])+2, anchor=SW ))
                self.ListLabels[len(self.ListLabels)-1].setLocation(0,(i+1)*25+5)
            for i in range(len(self.ListeChamps)):
                self.ListEntries.append(xEntry(self,width=self.ListeChamps[i][2]))
                self.ListEntries[len(self.ListEntries)-1].setLocation(120,(i+1)*25+5)
            self.ObtenirEnregistrement()
            self.Index = 0
            self.Afficher()
        else:
          msgBox.showerror('PERP 2009 | Édition des tables', 'Une erreur est survenue!\n'+Reponse[1])
          
    def close(self):
        self.destroy()
        pass        
    
    def Suivant(self):
        if (self.Index < len(self.ListeEnregistrement) - 1):
            self.Index = self.Index + 1
            self.Afficher()
    
    def Precedant(self):
        if (self.Index > 0):
            self.Index = self.Index - 1
            self.Afficher()
    
    def ObtenirEnregistrement(self):
        Requete = "select "
        for Champ in self.ListeChamps:
            Requete = Requete + Champ[0] + ", "
            
        Requete = Requete[:-2] +  " from " + self.CBTables.getText()
        print Requete
        Reponse = self.parent.serveur.ExecuterRequeteSQLWhere(Requete)
        print Reponse
        if(Reponse[0]==1):
          self.ListeEnregistrement = Reponse[1]
        else:
          msgBox.showerror('PERP 2009 | Édition des tables', 'Une erreur est survenue!\n'+Reponse[1])

    def Afficher(self):
        for i in range(len(self.ListeChamps)):
            self.ListEntries[i].setText(self.ListeEnregistrement[self.Index][i])
            



#class Controleur:
#    def __init__(self):
#        self.user = "ADMIN"
#        self.profil = "ADMIN"
#        Serveur='http://localhost:8000'
#        self.serveur=xmlrpclib.ServerProxy(Serveur)
#        root=Tk()
#        root.geometry('1200x600')
#        vi=Vue_EditionRapport(root,self)
#        root.mainloop()       
#
#Controleur = Controleur()
#    
    
